import React, { lazy, Suspense } from 'react';
import { createBrowserRouter } from 'react-router-dom';
import RootErrorBoundary from '@/pages/root-error-boundary'; // root错误边界
import RootLayout from '@/pages/layout';
import RootPage from '@/pages/page';

// lazy page
const NoMatchPage = lazy(() => import('@/pages/no-match'));
const TopicsLayout = lazy(() => import('@/pages/topics/layout'));
const TopicsPage = lazy(() => import('@/pages/topics/page'));
const TopicPage = lazy(() => import('@/pages/topic/page'));
const TopicUpsertPage = lazy(() => import('@/pages/topic/upsert/page'));
const LoginPage = lazy(() => import('@/pages/login/page'));

const router = createBrowserRouter([
    {
        path: '/',
        element: <RootLayout />,
        errorElement: <RootErrorBoundary />,
        children: [
            {
                index: true,
                element: <RootPage />,
            },
            {
                path: 'login',
                element: (
                    <Suspense fallback={<>...</>}>
                        <LoginPage />
                    </Suspense>
                ),
            },
            {
                path: 'topics',
                element: <TopicsLayout />,
                children: [
                    {
                        index: true,
                        element: (
                            <Suspense fallback={<>...</>}>
                                <TopicsPage />
                            </Suspense>
                        ),
                    },
                    {
                        path: 'good',
                        element: (
                            <Suspense fallback={<>...</>}>
                                <TopicsPage />
                            </Suspense>
                        ),
                    },
                    {
                        path: 'share',
                        element: (
                            <Suspense fallback={<>...</>}>
                                <TopicsPage />
                            </Suspense>
                        ),
                    },
                    {
                        path: 'ask',
                        element: (
                            <Suspense fallback={<>...</>}>
                                <TopicsPage />
                            </Suspense>
                        ),
                    },
                    {
                        path: 'job',
                        element: (
                            <Suspense fallback={<>...</>}>
                                <TopicsPage />
                            </Suspense>
                        ),
                    },
                    {
                        path: 'dev',
                        element: (
                            <Suspense fallback={<>...</>}>
                                <TopicsPage />
                            </Suspense>
                        ),
                    },
                ],
            },
            {
                path: 'topic/:id',
                element: (
                    <Suspense fallback={<>...</>}>
                        <TopicPage />
                    </Suspense>
                ),
            },
            {
                path: 'topic/create',
                element: (
                    <Suspense fallback={<>...</>}>
                        <TopicUpsertPage />
                    </Suspense>
                ),
            },
            {
                path: 'topic/:id/edit',
                element: (
                    <Suspense fallback={<>...</>}>
                        <TopicUpsertPage />
                    </Suspense>
                ),
            },
        ],
    },
    {
        path: '*',
        element: (
            <Suspense fallback={<>...</>}>
                <NoMatchPage />
            </Suspense>
        ),
    },
]);
if ((import.meta as any).hot) {
    // // console.log('hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh');
    (import.meta as any).hot.dispose(() => router.dispose());
}
export default router;
